package com.geek.demo01.controller;

import com.geek.demo01.config.JdbcProperties;
import com.geek.demo01.model.MyDatasource;
import com.geek.demo01.model.Student;
import com.geek.demo01.properties.WxProperties;
import com.geek.demo01.repository.StudentRepository;
import com.geek.demo01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName StudentController
 * @Description TODO
 * @Author zzm
 * @Data 2020/9/14 20:30
 * @Version 1.0
 */
@RestController
public class StudentController {

    @Autowired
    private WxProperties wxProperties;

    @Autowired
    private StudentService studentService;

    @Autowired
    private MyDatasource myDatasource;

    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/all")
    public List<Student> findAll() {
        List<Student> list = studentService.findAll();
        return list;
    }

    //路径参数 {id}
    //查询参数 ?id=
    @GetMapping("/test")
    public void test() {
        System.out.println(wxProperties.getAppid());
        System.out.println(wxProperties.getSecret());
    }

    @GetMapping("/test1")
    public void test1() {
        System.out.println(myDatasource.getDriver());
        System.out.println(myDatasource.getPassword());
    }

    @GetMapping("/find1")
    public List<Student> find1() {
        return studentRepository.findByAge(18);
    }

    @GetMapping("/find2/{name}")
    public Student find2(@PathVariable String name) {
        return studentRepository.findByName(name);
    }

    @GetMapping("/find3")
    public Page<Student> find3(@PageableDefault(page = 1, size = 20, sort = "age", direction = Sort.Direction.DESC) Pageable pageable) {
        PageRequest pageRequest = PageRequest.of(pageable.getPageNumber() - 1, pageable.getPageSize(), pageable.getSort());
        return studentRepository.findAll(pageRequest);
    }

    //根据学生`年龄`比较查询和`名称`、`班级`的模糊查询，并进行分页。
    @GetMapping("/find4")
    public Page<Student> find4(@PageableDefault(page = 1, size = 20) Pageable pageable,
                               @RequestParam Integer age, @RequestParam String keyword) {
        PageRequest pageRequest = PageRequest.of(pageable.getPageNumber() - 1, pageable.getPageSize());

        Specification spec = new Specification<Student>() {

            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();

                if (age != null) {
                    predicate = cb.equal(root.get("age"), age);
                }

                if (keyword != null) {
                    String key = "%" + keyword + "%";
                    predicate = cb.and(predicate, cb.or(
                            cb.like(root.get("name"), key),
                            cb.like(root.get("className"), key)));
                }

                return predicate;
            }
        };
        return studentRepository.findAll(spec, pageRequest);
    }

    @Autowired
    private JdbcProperties jdbcProperties;

    @GetMapping("/test5")
    public void test5() {
        System.out.println(jdbcProperties.getDriver());
        System.out.println(jdbcProperties.getPassword());
    }


    @GetMapping("/test6")
    public Map<String, String> test6() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "zhangsan");
        return map;
    }

}
